package com.atguigui.leetcode1;

/**
 * 1813.句子相似性 III
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/1/16 9:06
 */
public class P1813SentenceSimilarityIii {
    public static void main(String[] args) {
        Solution solution = new P1813SentenceSimilarityIii().new Solution();
        // TO TEST
    }

    class Solution {
        public boolean areSentencesSimilar(String sentence1, String sentence2) {
            String[] words1 = sentence1.split(" ");
            String[] words2 = sentence2.split(" ");
            int i = 0, j = 0;
            //i 表示两个字符串数组从左开始，最多有 i 个字符串相同。j 表示剩下的字符串数组从右开始，最多有 j 个字符串相同。
            //如果 i+j 正好是某个字符串数组的长度，那么原字符串就是相似的
            while (i < words1.length && i < words2.length && words1[i].equals(words2[i])) {
                i++;
            }
            while (j < words1.length - i && j < words2.length - i && words1[words1.length - j - 1].equals(words2[words2.length - j - 1])) {
                j++;
            }
            return i + j == Math.min(words1.length, words2.length);
        }
    }
}
